def dfs(bb:set, origA, A, B, choose, optimal):
    if len(bb) == 0:
        __ = set([choose[i][1] for i in range(len(choose))])
        value = sum([origA[i] for i in __])
        if value < optimal[0] or optimal[0] == -1:
            optimal[0] = value
            optimal[1] = choose.copy()
        return
    for b in bb:
        for idx in range(len(A)):
            if  B[b] < A[idx]:
                newbb = bb - set((b,))
                A[idx] -= B[b]
                choose.append((b, idx))
                dfs(newbb, origA, A, B, choose, optimal)
                choose.pop()
                A[idx] += B[b]

if __name__ == '__main__':
    A = [3.5, 6, 9.2, 10]
    B = [1, 2, 4.5]
    optimal = [-1, None]
    dfs(set(range(len(B))), A.copy(), A, B, [], optimal)
    with open('output.txt', 'w') as f:
        for idx in range(len(optimal[1])):
            f.write('{} {} {}\n'.format(B[optimal[1][idx][0]], optimal[1][idx][1], A[optimal[1][idx][1]]))
    print(optimal)
